package cinema.Controller;

import cinema.Dao.TimeTableDAO;
import cinema.Entity.TimeTable;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@SuppressWarnings("serial")
@Named
@ViewScoped
public class MainController implements Serializable {

    @Inject
    private TimeTableDAO timeTableDAO;

    private Date filterDate;

    private List<TimeTable> filteredTimeTables;

    @PostConstruct
    public void init() {
        filterDate = new Date();
        filter();
    }

    @SuppressWarnings("deprecation")
    public void filter() {
        filteredTimeTables = new ArrayList<TimeTable>();
        List<TimeTable> fullTimeTable = timeTableDAO.findAll();
        for (TimeTable t : fullTimeTable) {
            //időzóna miatt ki kell vonni 2 órát
            Calendar cal = Calendar.getInstance();
            cal.setTime(t.getStartTime());
            cal.add(Calendar.HOUR_OF_DAY, -2);
            Date time = cal.getTime();
            if (time.getDay() == filterDate.getDay() &&
                    time.getMonth() == filterDate.getMonth() &&
                    time.getYear() == filterDate.getYear())
                filteredTimeTables.add(new TimeTable(t));
        }
    }

    public Date getFilterDate() {
        return filterDate;
    }

    public void setFilterDate(Date filterDate) {
        this.filterDate = filterDate;
    }

    public List<TimeTable> getFilteredTimeTables() {
        return filteredTimeTables;
    }

    public void setFilteredTimeTables(List<TimeTable> filteredTimeTables) {
        this.filteredTimeTables = filteredTimeTables;
    }
}
